<template>
  <div class="container">
    <el-card class="box-card page-slide-left" shadow="never">
      <div slot="header" class="clearfix">
        <span>申请提现</span>
      </div>
      <el-form ref="withdrawForm" :model="withdrawForm" :rules="rules" label-width="0">
        <el-form-item prop="deviveList">

          <el-input
            v-model.trim="withdrawForm.deviveList"
            placeholder="请选择寄回设备SN"
            @click.native="openDrawer()"
            readonly
          >
            <i slot="suffix" class="suffix">
              <img src="../../assets/images/withdraw/2.png" alt="" width="20">
            </i>
          </el-input>
        </el-form-item>
        <el-form-item prop="logisticsOrderNo">
          <el-input v-model.trim="withdrawForm.logisticsOrderNo" placeholder="输入快递单号" @input="check()">
            <span slot="suffix" class="suffix" v-show="!withdrawForm.logisticsOrderNo">请输入</span>
          </el-input>
        </el-form-item>
        <el-form-item prop="logisticsCompany">
          <el-input v-model.trim="withdrawForm.logisticsCompany" placeholder="选择快递公司" @input="check()">
            <span slot="suffix" class="suffix" v-show="!withdrawForm.logisticsCompany">请输入</span>
          </el-input>
        </el-form-item>
      </el-form>
      <div class="bottomTips">
        <span>注：提现金额将打入绑定的个人微信账户中</span>
      </div>
    </el-card>
    <el-row style="margin-top: 14px;">
      <div class="checkOrders" @click="$router.push('/orderList')">
        <div>
          <img src="../../assets/images/withdraw/1.png" alt="" width="22">
          <span>查看提现订单</span>
        </div>
        <div>
          <img src="../../assets/images/withdraw/2.png" alt="" width="20">
        </div>
      </div>
    </el-row>
    <el-button class="themeButton"
               :disabled="disableFlag"
               @click="dialogVisible = true"
               @click.native="submit"
               style="margin-top: 16px;"
    >
      提交申请
    </el-button>
    <el-drawer
      :visible.sync="drawer"
      direction="btt"
      :show-close="false"
      size="50%"
    >
      <div slot="title" style="font-size: 15px; color: #1e2431; font-weight: bold;">
        可寄回设备
      </div>
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="item in deviceSN" :key="item.id"
                     :label="item.serialNumber"></el-checkbox>
      </el-checkbox-group>
      <el-button class="themeButton"
                 style="position: absolute; bottom: 12px; left: 50%; width: calc(100% - 30px); transform: translateX(-50%);"
                 @click="handleCloseDrawer"
      >
        提交申请
      </el-button>
    </el-drawer>

    <el-dialog
      :visible.sync="dialogVisible"
      :show-close="false"
      custom-class="successMsg"
    >
      <div slot="title">
        <img src="../../assets/images/withdraw/success.png" alt="" width="43">
      </div>
      <span style="font-size: 18px;
                   font-weight: bold;
                   text-align: center;
                   color: #333333;">
        提现申请发起成功，后台处理中!
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button class="themeButton"
                   @click="dialogVisible = false"
        >
      确定
         </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {getDeviceSN} from "@/api/deviceSN";
import {apply} from "@/api/withdrawLog";

export default {
  name: "withdraw",
  data() {
    return {
      withdrawForm: {
        deviveList: "",
        logisticsOrderNo: "",
        logisticsCompany: "",
        merchantId: ""
      },
      userToken: '',
      rules: {
        deviveList: [
          {required: true, message: " ", trigger: "blur"}
        ],
        logisticsOrderNo: [
          {required: true, message: " ", trigger: "blur"}
        ],
        logisticsCompany: [
          {required: true, message: " ", trigger: "blur"}
        ]
      },
      checkList: [],
      deviceSN: [],
      disableFlag: true,
      drawer: false,
      dialogVisible: false
    }
  },
  created() {
    const that = this
    that.withdrawForm.merchantId = localStorage.getItem("user").merchantId
    getDeviceSN().then(res => {
      console.log(res.data)
      that.deviceSN = res.data.data
      that.loading = false
    }).catch((err) => {
      this.$notify.error({
        title: '错误',
        message: err,
        showClose: false,
        duration: 1000
      });
    })

  },
  methods: {
    check() {
      let arr = []
      for (let key in this.withdrawForm) {
        arr.push(this.withdrawForm[key])
      }
      this.disableFlag = !arr.every((r) => r !== "")
    },
    openDrawer() {
      this.drawer = true
    },
    handleCloseDrawer() {
      this.drawer = false
      this.withdrawForm.deviveList = this.checkList.toString()
      console.log("closed")
    },
    submit() {
      const that = this;
      apply(
        that.$store.state.token,
        that.withdrawForm
      ).then(res => {
        try {
          console.log(res)
        } catch (err) {
          this.$notify.error({
            title: '错误',
            message: err,
            showClose: false,
            duration: 1000
          });
        } finally {
          that.dialogVisible = true
        }
      })
    }
  }
}

</script>

<style lang="scss" scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 100%;

  .clearfix {
    text-align: left;
    font-size: 16px;
    color: #1e2431;
    font-weight: bold;
  }
}

.el-card {
  box-sizing: border-box;
  padding: 0 16px;
  border: 0;

  & > > > .el-card__header {
    padding: 14px 0;
  }

  & > > > .el-card__body {
    padding: 0;
  }

  & > > > .el-input__inner {
    border-top: 0;
    border-left: 0;
    border-right: 0;
    height: 50px;
    line-height: 50px;
    padding: 0;
    border-radius: 0;
    border-bottom: 1px solid #EBEEF5;
  }

  & > > > .el-form-item {
    margin: 0;
  }
}

.suffix {
  display: flex;
  height: 50px;
  line-height: 50px;
  align-items: center;
}

.bottomTips {
  display: block;
  opacity: 0.6;
  font-size: 12px;
  color: #fd6e05;
  text-align: left;
  padding: 16px 0;
}

.checkOrders {
  div {
    display: flex;
    align-items: center;

    span {
      display: inline-block;
      padding-left: 7px;
      font-size: 14px;
      line-height: 20px;
      color: #333;
    }
  }

  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 8px 12px 12px;
  background-color: #fff;
  border-radius: 4px;
}

> > > .el-drawer {
  border-radius: 15px 15px 0 0;
}

.themeButton {
  width: 100%;
  height: 42px;
  color: #fff;
  opacity: 1;
  background: linear-gradient(90deg, rgba(253, 110, 5, 0.66) 10%, #fd6e05 81%);
  border-radius: 21px;
  border: none;
  transition: all .5s ease;

  &:hover,
  &:active {
    filter: brightness(1.1);
    box-shadow: 0 7px 25px 0 rgba(253, 110, 5, 0.41);
  }
}

.el-checkbox-group {
  padding: 0 15px;
  max-height: 30vh;
  overflow: scroll;

  .el-checkbox {
    padding: 14px 0;
    display: flex;
    justify-content: space-between;
    width: 100%;
    box-sizing: border-box;
    border-bottom: 1px solid #EBEEF5;
    flex-direction: row-reverse;

    > > > .el-checkbox__inner {
      border-radius: 50%;
    }
  }
}

> > > .successMsg {
  margin-top: 25vh !important;
  width: calc(100% - 30px) !important;
  border-radius: 12px;

  .el-dialog__header {
    padding: 30px 0 20px;
  }

  .el-dialog__body {
    width: 100%;
    padding: 0 0 30px;
    text-align: center;
  }

  .el-dialog__footer {
    padding: 0 34px 34px;
  }

  .themeButton {
    height: 36px;
  }
}

.el-button.is-disabled {
  opacity: 0.3;
}

.el-button.is-disabled:hover, .el-button.is-disabled:focus {
  color: #fff;
  background: linear-gradient(90deg, rgba(253, 110, 5, 0.66) 10%, #fd6e05 81%);;
}
</style>
